import javax.media.jai.*;
import java.awt.GridLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.image.RenderedImage;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.sun.media.jai.widget.DisplayJAI;


public class janelasSincronas extends JPanel implements AdjustmentListener{

	public static void main(String[] args) {
		
	}
		
	protected DisplayJAI dj1;
	protected DisplayJAI dj2;
	protected JScrollPane jsp1;
	protected JScrollPane jsp2;

	public janelasSincronas (RenderedImage im1, RenderedImage im2)
	{
		super();
		
		//criar o componente com duas JScrollPane
		setLayout (new GridLayout(1,2));
		dj1 = new DisplayJAI(im1);
		dj2 = new DisplayJAI(im2);
		jsp1 = new JScrollPane(dj1);
		jsp2 = new JScrollPane(dj2);
		add(jsp1);
		add(jsp2);
		
		//Listeners para scroll bars
		jsp1.getHorizontalScrollBar().addAdjustmentListener(this);
		jsp2.getHorizontalScrollBar().addAdjustmentListener(this);
		jsp1.getVerticalScrollBar().addAdjustmentListener(this);
		jsp2.getVerticalScrollBar().addAdjustmentListener(this);		
	}
	
	
	public void adjustmentValueChanged(AdjustmentEvent pos) {
		if (pos.getSource() == jsp1.getHorizontalScrollBar()) jsp2.getHorizontalScrollBar().setValue(pos.getValue());
		if (pos.getSource() == jsp2.getHorizontalScrollBar()) jsp1.getHorizontalScrollBar().setValue(pos.getValue());
		if (pos.getSource() == jsp1.getVerticalScrollBar()) jsp2.getVerticalScrollBar().setValue(pos.getValue());
		if (pos.getSource() == jsp2.getVerticalScrollBar()) jsp1.getVerticalScrollBar().setValue(pos.getValue());
		
		
	}

}
